﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using Comun;

namespace Guada
{
    public partial class FBase : Form
    {
        public DataTable _textosControlesDataTable;

        public FBase()
        {
            InitializeComponent();
        }

        #region Textos

        #region MappingTextControls
        protected void MappingTextControls(Form form, System.Windows.Forms.ToolTip ttToolTip)
        {

            System.Windows.Forms.ToolTip ctrlToolTip=new ToolTip();
            foreach (DataRow dr in this._textosControlesDataTable.Rows)
            {
                string sCtrlPA, sCtrlBD;

                // 1.- BUSCAMOS CONTROL EN LA PÁGINA

                sCtrlBD = dr["CONTROL"].ToString();
                sCtrlPA = Format.CodigoDeItem(sCtrlBD, "&");
                if (sCtrlPA == "") //No Es Grilla
                    sCtrlPA = sCtrlBD;
                Control ctrl=null;
                Control[] ctrls;
                ctrls=form.Controls.Find(sCtrlPA, true);
                if (ctrls.Length > 0)
                    ctrl = ctrls[0];
                else
                { 
                    // Verifico si es un tooltip
                    if (ttToolTip.Tag.ToString() == sCtrlPA)
                    {
                        ctrlToolTip = ttToolTip;
                        this.FindTextCtrlToolTip((System.Windows.Forms.ToolTip)ctrlToolTip, sCtrlBD, form);
                    }
                }
                
                // 2.- ESTABLECEMOS TEXTO DEL CONTROL 
                if (ctrl != null)
                {
                    switch (ctrl.GetType().UnderlyingSystemType.Name)
                    {
                            

                        case "Label":
                            this.FindTextCtrl((System.Windows.Forms.Label)ctrl);
                            break;
                        case "Button":
                            this.FindTextCtrl((System.Windows.Forms.Button)ctrl);
                            break;
                        case "DataGridView":
                            this.FindTextCtrl((System.Windows.Forms.DataGridView)ctrl, sCtrlBD);
                            break;
                        case "RadioButton":
                            this.FindTextCtrl((System.Windows.Forms.RadioButton)ctrl);
                            break;
                        case "CheckBox":
                            this.FindTextCtrl((System.Windows.Forms.CheckBox)ctrl);
                            break;
                        case "GroupBox":
                            this.FindTextCtrl((System.Windows.Forms.GroupBox)ctrl);
                            break;
                    }
                }
            }
        }
        #endregion

        #region GetText
        private string GetText(string ctrlName, string textoanterior)
        {

            DataRow[] selRow;
            if ((selRow = this._textosControlesDataTable.Select("CONTROL ='" + ctrlName + "'")) != null)
            {
                if (selRow.Length > 0)
                {
                    return (string)selRow[0]["TEXTO"];
                }
            }
            return textoanterior;

        }
        #endregion

        #region FindTextCtrl

        protected void FindTextCtrl(System.Windows.Forms.Button ctrl) 
        {
            ctrl.Text = this.GetText(ctrl.Name, ctrl.Text);
            //ctrl.ToolTip = this.GetText(ctrl.ID + ".ToolTip", ctrl.ToolTip);
        }

        protected void FindTextCtrl(System.Windows.Forms.Label ctrl)
        {
            ctrl.Text = this.GetText(ctrl.Name, ctrl.Text);
            //ctrl.ToolTip = this.GetText(ctrl.ID + ".ToolTip", ctrl.ToolTip);
        }

        protected void FindTextCtrl(System.Windows.Forms.GroupBox ctrl)
        {
            ctrl.Text = this.GetText(ctrl.Name, ctrl.Text);
        }

        protected void FindTextCtrl(System.Windows.Forms.RadioButton ctrl)
        {
            ctrl.Text = this.GetText(ctrl.Name, ctrl.Text);
            //ctrl.ToolTip = this.GetText(ctrl.ID + ".ToolTip", ctrl.ToolTip);
        }

        protected void FindTextCtrl(System.Windows.Forms.CheckBox ctrl)
        {
            ctrl.Text = this.GetText(ctrl.Name, ctrl.Text);
        }

        protected void FindTextCtrl(System.Windows.Forms.DataGridView ctrl, string sCampoBD)
        {
            int i;

            i = Convert.ToInt32(Format.DescripcionDeItem(sCampoBD, "&"));

            if (((System.Windows.Forms.DataGridView)ctrl).Columns.Count > i)
            {
                FindTextCtrl(ctrl.Columns[i], sCampoBD);
            }

        }
        protected void FindTextCtrlToolTip(System.Windows.Forms.ToolTip ctrl, string sCampoBD, Form form)
        {
            try
            {
                string sControl;
                Control[] ctrlsDest;
                Control ctrlDest;

                sControl = Format.DescripcionDeItem(sCampoBD, "&");

                ctrlsDest = form.Controls.Find(sControl, true);
                if (ctrlsDest.Length>0)
                {
                    ctrlDest = ctrlsDest[0];
                    ctrl.SetToolTip(ctrlDest, this.GetText(sCampoBD, ""));                    
                }
            }
            catch
            {}
        }

        protected void FindTextCtrl(System.Windows.Forms.DataGridViewColumn ctrl, string GridName)
        {
            try
            {
                ctrl.HeaderText = this.GetText(GridName, ctrl.HeaderText);
            }
            catch { }
        }

        protected void FindTextCtrl(Control ctrl)
        {

            switch (ctrl.GetType().UnderlyingSystemType.Name)
            {
                case "Label":
                    FindTextCtrl((System.Windows.Forms.Label)ctrl);
                    break;
                case "Button":
                    FindTextCtrl((System.Windows.Forms.Button)ctrl);
                    break;
                case "DataGrid":
                    FindTextCtrl((System.Windows.Forms.DataGrid)ctrl);
                    break;
                case "RadioButton":
                    this.FindTextCtrl((System.Windows.Forms.RadioButton)ctrl);
                    break;
                case "CheckBox":
                    this.FindTextCtrl((System.Windows.Forms.CheckBox)ctrl);
                    break;
                case "GroupBox":
                    this.FindTextCtrl((System.Windows.Forms.GroupBox)ctrl);
                    break;

            }
        }

        #endregion

        private void button1_Click(object sender, EventArgs e)
        {
            Conexion con = new Conexion();
            con.Conectar();
            con.Desconectar();
        }

        #endregion

    }
}

